extends Node

signal set_card_signal
signal player_turn_start
signal update_coin_signal
signal save_coin
signal return_gem_signal
signal update_opt_button
signal card_reset_signal
signal set_a_card
signal show_save_card_signal
signal get_noble_card
signal game_end_signal
signal player_get_gem_anim

var current_player: int = -1
const AI = preload("res://data/ai.tres")
const PLAYER = preload("res://data/player.tres")
var players: Array[PlayerState] = [PLAYER,AI]
var card_on_deck_node
var p_position = [
	[354,354],
	[1434,354]
]
# 场上可用宝石
var available_gems: Dictionary = {
	Globals.COIN_COLOR.WHITE:4,
	Globals.COIN_COLOR.BLUE:4,
	Globals.COIN_COLOR.RED:4,
	Globals.COIN_COLOR.GREEN:4,
	Globals.COIN_COLOR.BROWN:4,
	Globals.COIN_COLOR.GOLD:5,
} 
var card_decks: Dictionary = {
	1: [
		preload("res://data/cardBase/l1/card1.tres"),
		preload("res://data/cardBase/l1/card2.tres"),
		preload("res://data/cardBase/l1/card3.tres"),
		preload("res://data/cardBase/l1/card4.tres"),
		preload("res://data/cardBase/l1/card5.tres"),
		preload("res://data/cardBase/l1/card6.tres"),
		preload("res://data/cardBase/l1/card7.tres"),
		preload("res://data/cardBase/l1/card8.tres"),
		preload("res://data/cardBase/l1/card9.tres"),
		preload("res://data/cardBase/l1/card10.tres"),
		preload("res://data/cardBase/l1/card11.tres"),
		preload("res://data/cardBase/l1/card12.tres"),
		preload("res://data/cardBase/l1/card13.tres"),
		preload("res://data/cardBase/l1/card14.tres"),
		preload("res://data/cardBase/l1/card15.tres"),
		preload("res://data/cardBase/l1/card16.tres"),
		preload("res://data/cardBase/l1/card17.tres"),
		preload("res://data/cardBase/l1/card18.tres"),
		preload("res://data/cardBase/l1/card19.tres"),
		preload("res://data/cardBase/l1/card20.tres"),
		preload("res://data/cardBase/l1/card21.tres"),
		preload("res://data/cardBase/l1/card22.tres"),
		preload("res://data/cardBase/l1/card23.tres"),
		preload("res://data/cardBase/l1/card24.tres"),
		preload("res://data/cardBase/l1/card25.tres"),
		preload("res://data/cardBase/l1/card26.tres"),
		preload("res://data/cardBase/l1/card27.tres"),
		preload("res://data/cardBase/l1/card28.tres"),
		preload("res://data/cardBase/l1/card29.tres"),
		preload("res://data/cardBase/l1/card30.tres"),
		preload("res://data/cardBase/l1/card31.tres"),
		preload("res://data/cardBase/l1/card32.tres"),
		preload("res://data/cardBase/l1/card33.tres"),
		preload("res://data/cardBase/l1/card34.tres"),
		preload("res://data/cardBase/l1/card35.tres"),
		preload("res://data/cardBase/l1/card36.tres"),
		preload("res://data/cardBase/l1/card37.tres"),
		preload("res://data/cardBase/l1/card38.tres"),
		preload("res://data/cardBase/l1/card39.tres"),
		preload("res://data/cardBase/l1/card40.tres"),
	],  # 等级1卡组
	2: [
		preload("res://data/cardBase/l2/card41.tres"),
		preload("res://data/cardBase/l2/card42.tres"),
		preload("res://data/cardBase/l2/card43.tres"),
		preload("res://data/cardBase/l2/card44.tres"),
		preload("res://data/cardBase/l2/card45.tres"),
		preload("res://data/cardBase/l2/card46.tres"),
		preload("res://data/cardBase/l2/card47.tres"),
		preload("res://data/cardBase/l2/card48.tres"),
		preload("res://data/cardBase/l2/card49.tres"),
		preload("res://data/cardBase/l2/card50.tres"),
		preload("res://data/cardBase/l2/card51.tres"),
		preload("res://data/cardBase/l2/card52.tres"),
		preload("res://data/cardBase/l2/card53.tres"),
		preload("res://data/cardBase/l2/card54.tres"),
		preload("res://data/cardBase/l2/card55.tres"),
		preload("res://data/cardBase/l2/card56.tres"),
		preload("res://data/cardBase/l2/card57.tres"),
		preload("res://data/cardBase/l2/card58.tres"),
		preload("res://data/cardBase/l2/card59.tres"),
		preload("res://data/cardBase/l2/card60.tres"),
		preload("res://data/cardBase/l2/card61.tres"),
		preload("res://data/cardBase/l2/card62.tres"),
		preload("res://data/cardBase/l2/card63.tres"),
		preload("res://data/cardBase/l2/card64.tres"),
		preload("res://data/cardBase/l2/card65.tres"),
		preload("res://data/cardBase/l2/card66.tres"),
		preload("res://data/cardBase/l2/card67.tres"),
		preload("res://data/cardBase/l2/card68.tres"),
		preload("res://data/cardBase/l2/card69.tres"),
		preload("res://data/cardBase/l2/card70.tres"),
	],
	3: [
		preload("res://data/cardBase/l3/card71.tres"),
		preload("res://data/cardBase/l3/card72.tres"),
		preload("res://data/cardBase/l3/card73.tres"),
		preload("res://data/cardBase/l3/card74.tres"),
		preload("res://data/cardBase/l3/card75.tres"),
		preload("res://data/cardBase/l3/card76.tres"),
		preload("res://data/cardBase/l3/card77.tres"),
		preload("res://data/cardBase/l3/card78.tres"),
		preload("res://data/cardBase/l3/card79.tres"),
		preload("res://data/cardBase/l3/card80.tres"),
		preload("res://data/cardBase/l3/card81.tres"),
		preload("res://data/cardBase/l3/card82.tres"),
		preload("res://data/cardBase/l3/card83.tres"),
		preload("res://data/cardBase/l3/card84.tres"),
		preload("res://data/cardBase/l3/card85.tres"),
		preload("res://data/cardBase/l3/card86.tres"),
		preload("res://data/cardBase/l3/card87.tres"),
		preload("res://data/cardBase/l3/card88.tres"),
		preload("res://data/cardBase/l3/card89.tres"),
		preload("res://data/cardBase/l3/card90.tres"),
	],
	'noble': []
}
var unused_cards: Dictionary = {
	1: [],  # 已翻开卡牌（每等级4张）
	2: [],
	3: [],
	'noble': []
}
var revealed_cards: Dictionary = {
	1: [],  # 已翻开卡牌（每等级4张）
	2: [],
	3: [],
	'noble': []
}
var select_card

var noble_cards = [
	preload("res://data/cardBase/nobe/card91.tres"),
	preload("res://data/cardBase/nobe/card92.tres"),
	preload("res://data/cardBase/nobe/card93.tres"),
	preload("res://data/cardBase/nobe/card94.tres"),
	preload("res://data/cardBase/nobe/card95.tres"),
	preload("res://data/cardBase/nobe/card96.tres"),
	preload("res://data/cardBase/nobe/card97.tres"),
	preload("res://data/cardBase/nobe/card98.tres"),
	preload("res://data/cardBase/nobe/card99.tres"),
	preload("res://data/cardBase/nobe/card100.tres")
]
var noble_in_deck = []

func setup_game(num_players: int):
	for item in players:
		pass
	noble_cards.shuffle()
	noble_in_deck = noble_cards.slice(0,3)
	for item in unused_cards:
		unused_cards[item] = []
		unused_cards[item] = card_decks[item]
		unused_cards[item].shuffle()
	var a=[]
	for i in 3:
		for j in 4:
			var card = unused_cards[i+1].pop_back()
			revealed_cards[i+1].append(card)
			set_card(i+1,i*4+j,card)
	start_player_turn(0)
	update_coin_signal.emit()

func set_card(level:int,position:int,card:Resource):
	set_card_signal.emit({
		'level':level,
		'position':position,
		'card':card
	})

func start_player_turn(player_index: int):
	if current_player != -1:
		check_get_noble_card()
		check_victory()
	#切换玩家
	current_player = player_index
	player_turn_start.emit(player_index)

func handle_player_action(action:int, player:PlayerState):
	# 处理玩家操作（拿取宝石/购买卡牌等） 0-拿宝石 1-预留 2-购买
	if action == 0:
		GameManager.player_get_gem_anim.emit()
		for idx in player.save_coin.size():
			player.gems[idx] += player.save_coin[idx]
			if player == players[1]:
				available_gems[idx] -= player.save_coin[idx]
			player.save_coin[idx] = 0
		update_opt_button.emit()
		update_coin_signal.emit()
		GameManager.start_player_turn( abs(GameManager.current_player - 1) )
		
func player_save_card(player:PlayerState, card):
	pass
	
func player_buy_card(player:PlayerState, card):
	for i in range(5):
		if player.save_coin[i] > 0:
			available_gems[i] += player.save_coin[i]
			player.save_coin[i] = 0
	var info:CardData = card.card_info
	var chajia = 0
	var cost = card.card_info.cost.duplicate()
	for item in player.bonuses:
		var obj = player.bonuses
		if cost[item] > 0 && obj[item] > 0:
			cost[item] = 0 if cost[item] - obj[item] < 0 else cost[item] - obj[item]
	for item in cost:
		if cost[item] > player.gems[item]:
			chajia += cost[item] - player.gems[item]
		available_gems[item] += cost[item] if player.gems[item] - cost[item] >=0 else player.gems[item]
		player.gems[item] = player.gems[item] - cost[item] if player.gems[item] - cost[item] >=0 else 0
		
	player.gems[5] -= chajia
	available_gems[5] += chajia
	player.bonuses[info.bonus_color] += 1
	player.prestige_points += info.prestige
	var idx = card.idx
	update_coin_signal.emit()
	var tween = get_tree().create_tween()
	tween.tween_property(card, "position", Vector2(p_position[current_player][0],p_position[current_player][1]), 0.3)
	await tween.finished
	card.queue_free()
	await get_tree().create_timer(1.5).timeout
	set_a_card.emit(idx)
	GameManager.start_player_turn( abs(GameManager.current_player - 1) )

func check_get_noble_card():
	var c_player = players[current_player]
	for i in range(noble_in_deck.size()-1,-1,-1):
		var card_info = noble_in_deck[i]
		var flag = true
		for item in c_player.bonuses:
			if c_player.bonuses[item] < card_info.cost[item]:
				flag = false
		if flag:
			print('当前玩家获得',card_info.card_id,'贵族卡')
			c_player.prestige_points += card_info.prestige
			GameManager.get_noble_card.emit(card_info)
			noble_in_deck.remove_at(i)

func check_victory():
	for player in players:
		if player.prestige_points >= 15:
			prints('获胜',current_player)
			game_end_signal.emit()
